﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Tranquillity;
using Tranquillity.Helpers;

namespace TranquillityDemos
{
    /// <summary>
    /// A fire emitter
    /// </summary>
    public class ExplosionEmitter  : IParticleEmitter
    {
        public DynamicParticleSystem ParticleSystem { get; set; }

        public int EmissionRate { get; set; }

        private Vector3 position;

        public float Scale { get; set; }

        public Vector3 Velocity { get; set; }
        Vector3 velocity;
        
        const int numExplosionParticles = 2;

        
        int CountDown = 0;
        public ExplosionEmitter (int emissionRate)
        {
            EmissionRate = emissionRate;
            position = Vector3.Zero;
            velocity = Vector3.Zero;
            Scale = 1.0f;
            
        }
        public void Explode(int Time,Vector3 position, float scale)
        {
            CountDown = Time;
            this.position = position;
            Scale = scale;
            velocity = Vector3.Zero;
        }

        public void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            position += velocity * elapsedTime;
            Emit(EmissionRate);
        }

        public void Emit(int particlesToEmit)
        {
            if (CountDown <= 0)
            {
                return;
            }
            else
            {
                CountDown--;
            }
            for (int i = 0; i < numExplosionParticles; i++)
            {
                ParticleSystem.AddParticle(
                    position,
                    RandomHelper.ColorBetween(Color.DarkGray, Color.Gray),
                    /*velocity * 0.01f +*/ new Vector3(RandomHelper.FloatBetween(-30, 30), RandomHelper.FloatBetween(30, -10), RandomHelper.FloatBetween(-30, 30)) * 0.05f,
                    //velocity,
                    RandomHelper.FloatBetween(-0.01f, 0.01f),
                    TimeSpan.FromSeconds(RandomHelper.IntBetween(1, 2)),
                    true,
                    RandomHelper.FloatBetween(0.0f, MathHelper.Pi),
                    Scale);
            }
                
        }
    }
}
